package cn.edu.tju.elm.controller;

import cn.edu.tju.elm.common.response.HttpResult;
import cn.edu.tju.elm.model.entity.Order;
import cn.edu.tju.elm.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/orders")
@Tag(name = "订单管理", description = "提供订单的增删改查功能，包括创建订单、查询订单、更新订单状态等")
public class OrderController {
    private final OrderService ordersService;

    public OrderController(OrderService ordersService) {
        this.ordersService = ordersService;
    }

    @PostMapping(value = "")
    @Operation(
            summary = "创建订单",
            description = "将当前用户的购物车内容创建为订单，需要指定商家和配送地址"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "订单创建成功"),
            @ApiResponse(responseCode = "400", description = "请求参数无效，如不满足起送价等"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "404", description = "商家或配送地址不存在")
    })
    public HttpResult<Order> addOrder(
            @Parameter(description = "订单信息", required = true,
                    examples = @ExampleObject(
                            value = """
                                {
                                    "business": {
                                        "id": 3
                                    },
                                    "deliveryAddress": {
                                        "id": 2
                                    }
                                }
                            """
                    ))
            @RequestBody Order order) {
        return HttpResult.ok(ordersService.addOrder(order));
    }

    @Operation(
            summary = "根据订单ID获取订单详情",
            description = "根据订单ID获取指定订单的详细信息，只能获取当前用户自己的订单"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "只能查询自己的订单"),
            @ApiResponse(responseCode = "404", description = "订单不存在")
    })
    @GetMapping("/{id}")
    public HttpResult<Order> getOrderById(
            @Parameter(description = "订单ID", required = true, example = "1")
            @PathVariable Long id) {
        return HttpResult.ok(ordersService.getOrderById(id));
    }

    @GetMapping("/users")
    @Operation(
            summary = "获取当前用户的订单列表",
            description = "根据当前登录用户的token获取其所有的订单记录"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问")
    })
    public HttpResult<List<Order>> listOrdersByUserId() {
        return HttpResult.ok(ordersService.listOrdersByUser());
    }

    @PatchMapping()
    @Operation(
            summary = "更新订单状态或收货地址",
            description = "部分更新订单信息，仅允许更新订单状态和配送地址"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "只能更新自己的订单"),
            @ApiResponse(responseCode = "404", description = "订单或配送地址不存在")
    })
    public HttpResult<Order> patchOrder(
            @Parameter(description = "订单更新信息", required = true,
                    examples = @ExampleObject(
                            value = """
                                {
                                    "id": 73,
                                    "deliveryAddress": {
                                        "id": 78
                                    },
                                    "orderState": 1
                                }
                            """
                    ))
            @RequestBody Order order){
        return HttpResult.ok(ordersService.patchOrder(order));
    }
}